<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-row v-for="(item) in schoolOptions"
            :key="item.schoolId"
            @click.native="handleClickSchool(item)" >
            <el-col class="school_list">
              {{ item.schoolName }}
            </el-col>
          </el-row>
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="学院代码" prop="collegeCode">
            <el-input
              v-model="queryParams.collegeCode"
              placeholder="请输入学院代码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="学院名称" prop="collegeName">
            <el-input
              v-model="queryParams.collegeName"
              placeholder="请输入学院名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['edu:bColleges:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['edu:bColleges:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['edu:bColleges:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['edu:bColleges:export']"
            >导出</el-button>
          </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table ref="table" 
          stripe
          border 
          v-loading="loading" :data="bcollegesList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <!--<el-table-column label="学院ID" align="center" prop="collegeId" />-->
          <el-table-column type="expand" width="1">
            <template slot-scope="scope">
              <el-table
              :data="scope.row.bMajorsList"
              style="width: 100%">
              <!--<el-table-column label="专业ID" align="center" prop="majorId" />-->
              <el-table-column label="专业代码" align="center" prop="majorCode" />
              <el-table-column label="专业名称" align="center" prop="majorName" />
              <el-table-column label="排序" align="center" prop="sort" />
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdateMajor(scope.row)"
                    v-hasPermi="['edu:hColleges:edit']"
                  >修改</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDeleteMajor(scope.row)"
                    v-hasPermi="['edu:hColleges:remove']"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            </template>
            
          </el-table-column>
          <el-table-column label="学院代码" align="center" prop="collegeCode" />
          <el-table-column label="学院名称" align="center" prop="collegeName" />
          <el-table-column label="状态" :formatter="statusFormat" align="center" prop="status" />
          <el-table-column label="排序" align="center" prop="sort" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleAddMajor(scope.row)"
                v-hasPermi="['edu:hColleges:edit']"
              >新增专业</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="expandChange(scope.row)"
                v-hasPermi="['edu:hColleges:edit']"
              >查看专业</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['edu:bColleges:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['edu:bColleges:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    

    <!-- 添加或修改本科学校学院信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="学校" prop="schoolId">
          <el-select
              v-model="form.schoolId"
              placeholder="请选择学校"
              clearable
              size="small"
              style="width: 200px"
            >
              <el-option
                v-for="school in schoolOptions"
                :key="school.schoolId"
                :label="school.schoolName"
                :value="school.schoolId"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="学院代码" prop="collegeCode">
          <el-input v-model="form.collegeCode" placeholder="请输入学院代码" />
        </el-form-item>
        <el-form-item label="学院名称" prop="collegeName">
          <el-input v-model="form.collegeName" placeholder="请输入学院名称" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序" />
            </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
          </el-select>
        </el-form-item>
        <!--
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改专业信息对话框 -->
    <el-dialog :title="title" :visible.sync="openMajor" width="800px" append-to-body>
      <el-form ref="formMajor" :model="formMajor" :rules="rulesMajor" label-width="80px">
        <el-form-item label="专业代码" prop="majorCode">
          <el-input v-model="formMajor.majorCode" placeholder="请输入专业代码" />
        </el-form-item>
        <el-form-item label="专业名称" prop="majorName">
          <el-input v-model="formMajor.majorName" placeholder="请输入专业名称" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序" />
            </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormMajor">确 定</el-button>
        <el-button @click="cancelMajor">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBcolleges, getBcolleges, delBcolleges, addBcolleges, updateBcolleges, exportBcolleges } from "@/api/edu/bColleges";
import { listSchool } from "@/api/edu/bSchools";
import {getBmajors, delBmajors, addBmajors, updateBmajors } from "@/api/edu/bMajors";

export default {
  name: "Bcolleges",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 本科学校学院信息表格数据
      bcollegesList: [],
      schoolOptions: [],
      statusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openMajor: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolId: null,
        collegeCode: null,
        collegeName: null,
        status: null,
      },
      // 表单参数
      form: {},
      formMajor: {},
      // 表单校验
      rules: {
        schoolId: [
          { required: true, message: "院校ID不能为空", trigger: "blur" }
        ],
        collegeCode: [
          { required: true, message: "学院代码不能为空", trigger: "blur" }
        ],
        collegeName: [
          { required: true, message: "学院名称不能为空", trigger: "blur" }
        ],
      },
      rulesMajor: {
        majorCode: [
          { required: true, message: "专业代码不能为空", trigger: "blur" }
        ],
        majorName: [
          { required: true, message: "专业名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.queryBSchool();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    queryBSchool() {
      listSchool().then(response => {
        this.schoolOptions = response.data
      })
    },
    handleClickSchool(item) {
      this.queryParams.schoolId = item.schoolId
      this.handleQuery()
    },
    /** 查询本科学校学院信息列表 */
    getList() {
      this.loading = true;
      listBcolleges(this.queryParams).then(response => {
        this.bcollegesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelMajor() {
      this.openMajor = false;
      this.resetMajor();
    },
    // 表单重置
    reset() {
      this.form = {
        collegeId: null,
        schoolId: null,
        schoolName: null,
        collegeCode: null,
        collegeName: null,
        sort: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    resetMajor() {
      this.formMajor = {
        collegeId: null,
        majorId: null,
        majorCode: null,
        majorName: null,
        sort: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("formMajor");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.collegeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    expandChange(row) {
      this.$refs.table.toggleRowExpansion(row)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加本科学校学院信息";
    },
    handleAddMajor(row) {
      this.resetMajor()
      this.formMajor.collegeId = row.collegeId
      this.openMajor = true
      this.title = "添加专业信息";
      
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const collegeId = row.collegeId || this.ids
      getBcolleges(collegeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改本科学校学院信息";
      });
    },
    handleUpdateMajor(row) {
      this.reset();
      const majorId = row.majorId
      getBmajors(majorId).then(response => {
        this.formMajor = response.data;
        this.openMajor = true;
        this.title = "修改专业信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.schoolId != null) {
            this.queryParams.schoolId = this.form.schoolId
          }

          if (this.form.collegeId != null) {
            updateBcolleges(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBcolleges(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const collegeIds = row.collegeId || this.ids;
      this.$confirm('是否确认删除本科学校学院信息编号为"' + collegeIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBcolleges(collegeIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    submitFormMajor() {
      this.$refs["formMajor"].validate(valid => {
        if (valid) {
          if (this.formMajor.majorId != null) {
            updateBmajors(this.formMajor).then(response => {
              this.msgSuccess("修改成功");
              this.openMajor = false;
              this.getList();
            });
          } else {
            addBmajors(this.formMajor).then(response => {
              this.msgSuccess("新增成功");
              this.openMajor = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDeleteMajor(row) {
      const majorIds = row.majorId;
      this.$confirm('是否确认删除专业信息编号为"' + majorIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBmajors(majorIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有本科学校学院信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportBcolleges(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
<style scoped>
.school_list {
    padding-left: 0px;
    margin: 0 5px;
    height: 40px;
    display: flex;
    -webkit-box-align: center;
    align-items: center;
    cursor: pointer;

}
</style>